package cn.caipengbo.validatecode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Myth on 1/21/2017.
 */
@WebServlet(name = "CheckValidateCode")
public class CheckValidateCode extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得表单code
        String formcode = request.getParameter("code").toLowerCase();
        //获得session中code
        String sessioncode = ((String) request.getSession().getAttribute("code")).toLowerCase();
        //检查是否一致
        if (formcode!=null && sessioncode!=null&&formcode.equals(sessioncode)){
            //成功
            response.sendRedirect("/JavaWeb/index.jsp");
        } else {
            //失败
            request.setAttribute("errorinfo","验证码错误");
            request.getRequestDispatcher("/login.jsp").forward(request,response);//请求转发是服务器的行为，所以是服务器路径
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
